package com.example.fw;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;


public class ContactHelper extends HelperWithWebDriverBase {


	private Contacts contactCache;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	public Contacts getContacts() {
		if (contactCache == null) {
			buildContactsCache();
		} 
		return contactCache;
	}
	private void buildContactsCache() {
		manager.getNavigationHelper().openMainPage();
		WebElement form = findElements(By.tagName("form")).get(2);
		List<WebElement> checkboxes = form.findElements(By.name("selected[]"));
		Set<ContactObject> contacts = new HashSet<ContactObject>();
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			title = stripString(title);
			ContactObject contact = new ContactObject().setFullName(title)
					.setId(checkbox.getAttribute("value"));
			contacts.add(contact);
		}
		contactCache = new Contacts(contacts);
	}
	protected void submitContact() {
		click(By.name("submit"));
	}

	protected void fillContactForm(ContactObject contactObject,
			Boolean selectGroup) {
		type("firstname", contactObject.firstname);
		type("lastname", contactObject.lastname);
		type("address", contactObject.address);
		type("home", contactObject.home);
		type("mobile", contactObject.mobile);
		type("work", contactObject.work);
		type("email", contactObject.email);
		type("email2", contactObject.email2);
		selectValue("bday", contactObject.bDay);
		selectValue("bmonth", contactObject.bMonth);
		type("byear", contactObject.bYear);
		if (selectGroup) {
			selectRandom(By.name("new_group"));
		} else {
			if (findElements(By.name("new_group")).size() > 0) {
				throw new Error(
						"Dropdown for group selection exists, but it should not!");
			}
		}
		type("address2", contactObject.address2);
		type("phone2", contactObject.phone2);
	}

	public void createContact(ContactObject validGroup) {
		manager.getNavigationHelper().initContactCreation();
		fillContactForm(validGroup, true);
		submitContact();
		manager.getNavigationHelper().returnToHomePage();
		buildContactsCache();
	}

	public void editContact(ContactObject validContact) {
		manager.getNavigationHelper().openMainPage();
		selectElement("selected[]",0);
		click(By.cssSelector("img[alt=\"Edit\"]"));
		fillContactForm(validContact, false);
		click(By.xpath("(//input[@name='update'])[1]"));
		manager.getNavigationHelper().returnToHomePage();
		contactCache = null;
	}

	public void deleteContact() {
		manager.getNavigationHelper().openMainPage();
		selectElement("selected[]",0);
		click(By.cssSelector("img[alt=\"Edit\"]"));
		click(By.xpath("(//input[@name='update'])[2]"));
		manager.getNavigationHelper().returnToHomePage();
		contactCache = null;
	}
	// load all groups from the dropdown
	public List<String> getGroupsList() {
		List<String> groups = new ArrayList<String>();
		WebElement dropdown = findElement(By.name("new_group"));
		Select select = new Select(dropdown);
		List<WebElement> options = select.getOptions();
		for (WebElement we : options) {
			groups.add(we.getText());
		}
		return groups;
	}

	public ContactObject getFirstContact() {
		WebElement checkbox = findElements(By.name("selected[]")).get(0);
		String title = checkbox.getAttribute("title");
		title = stripString(title);
		ContactObject contact = new ContactObject().setFullName(title).setId(
				checkbox.getAttribute("value"));
		return contact;
	}
}
